home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / cti_test.pro < prev    next >
Text File  |  1997-07-08  |  6KB  |  152 lines

  1. ;$Id: cti_test.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       CTI_TEST
  8. ;
  9. ; PURPOSE:
  10. ;       This function constructs a "contingency table" from an array of
  11. ;       observed frequencies and tests the hypothesis that the rows and
  12. ;       columns are independent using an extension of the chi-squared 
  13. ;       goodness-of-fit test. The result is a two-element vector contain-
  14. ;       ing the chi-squared test statistic X2 and probability of obtaining 
  15. ;       a value of X2 or greater.
  16. ;
  17. ; CATEGORY:
  18. ;       Statistics.
  19. ;
  20. ; CALLING SEQUENCE:
  21. ;       Result = CTI_TEST(OBFREQ)
  22. ;
  23. ; INPUTS:
  24. ;       OBFREQ:  An array of c-columns and r-rows of type integer, float 
  25. ;                or double containing observed frequencies.
  26. ;
  27. ; KEYWORD PARAMETERS:
  28. ;        COEFF:  Use this keyword to specify a named variable which returns
  29. ;                the Coefficient of Contingency. A non-negative scalar,
  30. ;                in the interval [0.0, 1.0], which measures the degree
  31. ;                of dependence within a contingency table. The larger the
  32. ;                value of COEFF, the greater the degree of dependence.
  33. ;
  34. ;    CORRECTED:  If set to a nonzero value, the "Yate's Correction for
  35. ;                Continuity" is used to compute the chi-squared test 
  36. ;                statistic, X2. The Yate's correction always decreases the
  37. ;                magnitude of the chi-squared test statistic, X2. In general,
  38. ;                this keyword should be set for small sample sizes.
  39. ;
  40. ;         CRAMV: Use this keyword to specify a named variable which returns
  41. ;                Cramer's V. A non-negative scalar, in the interval [0.0, 1.0],
  42. ;                which measures the degree of dependence within a contingency
  43. ;                table.
  44. ;
  45. ;           DF:  Use this keyword to specify a named variable which returns
  46. ;                the degrees of freedom used to compute the probability of 
  47. ;                obtaining the value of the chi-squared test statistic or
  48. ;                greater. DF = (r - 1) * (c - 1) where r and c are the
  49. ;                number of rows and columns of the contingency table, 
  50. ;                respectively.
  51. ;
  52. ;       EXFREQ:  Use this keyword to specify a named variable which returns
  53. ;                an array of c-columns and r-rows containing expected  
  54. ;                frequencies. The elements of this array are often refered
  55. ;                to as the "cells" of the expected frequencies. The expected
  56. ;                frequency of each cell is computed as the product of row
  57. ;                and column marginal frequencies divided by the overall 
  58. ;                total of observed frequencies.
  59. ;     RESIDUAL:  Use this keyword to specify a named variable which returns
  60. ;                an array of c-columns and r-rows containing signed differences
  61. ;                between corresponding cells of observed frequencies and 
  62. ;                expected frequencies.
  63. ;
  64. ; EXAMPLE:
  65. ;       Define the 5-column and 4-row array of observed frequencies.
  66. ;         obfreq = [[748, 821, 786, 720, 672], $
  67. ;                   [ 74,  60,  51,  66,  50], $
  68. ;                   [ 31,  25,  22,  16,  15], $
  69. ;                   [  9,  10,   6,   5,   7]]
  70. ;       Test the hypothesis that the rows and columns of "obfreq" contain
  71. ;       independent data at the 0.05 significance level.
  72. ;         result = cti_test(obfreq, coeff = coeff)
  73. ;       The result should be the two-element vector [14.3953, 0.276181].
  74. ;       The computed value of 0.276181 indicates that there is no reason to
  75. ;       reject the proposed hypothesis at the 0.05 significance level.
  76. ;       The Coefficient of Contingency returned in the parameter "coeff" 
  77. ;       (coeff = 0.0584860) also indicates the lack of dependence between
  78. ;       the rows and columns of the observed frequencies. Setting the 
  79. ;       CORRECTED keyword returns the two-element vector [12.0032, 0.445420]
  80. ;       and (coeff = 0.0534213) resulting in the same conclusion of 
  81. ;       independence.
  82. ;
  83. ; PROCEDURE:
  84. ;       CTI_TEST constructs a "contingency table" from a 2-dimensional
  85. ;       input array of observed frequencies and applies the principles of
  86. ;       the chi-squared goodness-of-fit test to determine the independence
  87. ;       of the rows and columns. For small sample sizes, a correction for
  88. ;       absolute differences between observed and expected frequencies may
  89. ;       be applied by setting the CORRECTED keyword.
  90. ;
  91. ; REFERENCE:
  92. ;       PROBABILITY and STATISTICS for ENGINEERS and SCIENTISTS (3rd edition)
  93. ;       Ronald E. Walpole & Raymond H. Myers
  94. ;       ISBN 0-02-424170-9
  95. ;
  96. ; MODIFICATION HISTORY:
  97. ;       Written by:  GGS, RSI, August 1994
  98. ;-
  99.  
  100. function cti_test, obfreq, corrected = corrected, coeff = coeff, $
  101.                            cramv = cramv, df = df, $
  102.                            exfreq = exfreq, residual = residual
  103.  
  104.   on_error, 2
  105.  
  106.   ;Size attributes for array of observed values.
  107.   sobfreq = size(obfreq)
  108.  
  109.   if sobfreq[0] ne 2 then message, $
  110.     'Observed frequencies must be two-dimensional array.'
  111.  
  112.   ineg = where(obfreq lt 0, nneg)
  113.   if nneg ne 0 then message, $
  114.     'Array of observed frequencies cannot contain negative data.'
  115.  
  116.   col = sobfreq[1] ;Column dimension.
  117.   row = sobfreq[2] ;Row dimension.
  118.  
  119.   obtotal = total(obfreq) ;Overall total.
  120.  
  121.   ;Marginal frequencies.
  122.   coltotal = total(obfreq, 2)
  123.   rowtotal = total(obfreq, 1)
  124.  
  125.   ;Expected frequencies.
  126.   exfreq = coltotal # rowtotal / obtotal  
  127.  
  128.   ;Degrees of freedom
  129.   df = (row - 1) * (col - 1)
  130.  
  131.   if keyword_set(corrected) ne 0 then begin
  132.     ;Apply Yate's Correction.
  133.     residual = abs(obfreq - exfreq) 
  134.     z = total((residual - 0.5)^2.0 / exfreq)
  135.     prob = 1 - chisqr_pdf(z, df)
  136.   endif else begin
  137.     residual = (obfreq - exfreq)
  138.     z = total(residual^2.0 / exfreq)
  139.     prob = 1 - chisqr_pdf(z, df)
  140.   endelse
  141.  
  142.   ;Coefficient of contingency.
  143.   coeff = sqrt(z / (z + obtotal)) 
  144.  
  145.   ;Cramer's V.
  146.   cramv = sqrt(z / (obtotal * min([row - 1, col - 1])))
  147.  
  148.   return, [z, prob]
  149.  
  150. end
  151.